package com.rms.hkpc.fragment;

import android.content.Intent;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

import com.rms.hkpc.R;
import com.rms.hkpc.activity.AddHocActivity;
import com.rms.hkpc.activity.ImagePreviewActivity;
import com.rms.hkpc.adapter.InsightAdapter;
import com.rms.hkpc.adapter.ResultAdapter;
import com.rms.hkpc.adapter.ShowAdapter;
import com.rms.hkpc.adapter.WorkOrderInsightAdapter;
import com.rms.hkpc.adapter.base.BaseAdapter;
import com.rms.hkpc.apiresult.ApiResultData;
import com.rms.hkpc.constant.AllConstants;
import com.rms.hkpc.control.GridLayoutManager;
import com.rms.hkpc.control.LinearLayoutManager;
import com.rms.hkpc.db.bean.Event;
import com.rms.hkpc.db.dto.InsightDto;
import com.rms.hkpc.db.dto.ParamValDto;
import com.rms.hkpc.db.dto.StatusUseRateDto;
import com.rms.hkpc.db.dto.WorkOrderPhotosDto;
import com.rms.hkpc.db.dto.WorksOrderDto;
import com.rms.hkpc.db.query.BaseQuery;
import com.rms.hkpc.fragment.base.BaseListFragment;
import com.rms.hkpc.presenter.InsightPresenter;
import com.rms.hkpc.util.OKHttpUtil;
import com.rms.hkpc.view.IInsightView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class InsightFragment extends BaseListFragment<IInsightView, InsightPresenter, StatusUseRateDto, BaseQuery> implements IInsightView {

    private InsightAdapter adapter;
    private List<ParamValDto> list,listShow;
    private List<WorksOrderDto> listWorkOrder;
    private WorkOrderInsightAdapter adapterWorkOrder;
    private ResultAdapter adapterResult;
    private ShowAdapter adapterShow;
    private boolean isShow = true,isThread = true;
    private static Thread thread;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_insight;
    }

    @Override
    protected InsightPresenter createPresenter() {
        return new InsightPresenter();
    }

    @Override
    protected BaseQuery createQuery() {
        return new BaseQuery();
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        smartRefreshLayout.setEnableLoadMore(false);
//        Result
        RecyclerView rvResult = fd(R.id.rv_result);
        rvResult.setNestedScrollingEnabled(false);
        rvResult.setItemAnimator(null);
        list = new ArrayList<>();
        adapterResult = new ResultAdapter(context, list, R.layout.rvitem_insight);
        rvResult.setLayoutManager(new GridLayoutManager(context, 2));
        rvResult.setAdapter(adapterResult);
//        Show
        RecyclerView rvShow = fd(R.id.rv_show);
        rvShow.setNestedScrollingEnabled(false);
        rvShow.setItemAnimator(null);
        listShow = new ArrayList<>();
        adapterShow = new ShowAdapter(context, listShow, R.layout.rvitem_show);
        rvShow.setLayoutManager(new LinearLayoutManager(context));
        rvShow.setAdapter(adapterShow);
//        Status
        adapter = new InsightAdapter(context, mData, R.layout.rvitem_insight);
        recyclerView.setLayoutManager(new GridLayoutManager(context, 3));
        recyclerView.setAdapter(adapter);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setItemAnimator(null);
//        WorkOrder
        RecyclerView rvWorkOrder = fd(R.id.rv_work_order);
        rvWorkOrder.setNestedScrollingEnabled(false);
        rvWorkOrder.setItemAnimator(null);
        listWorkOrder = new ArrayList<>();
        adapterWorkOrder = new WorkOrderInsightAdapter(getActivity(),context, listWorkOrder, R.layout.rvitem_work_order_insight);
        adapterWorkOrder.setOnItemChildrenClickListener(new BaseAdapter.OnItemChildrenClickListener() {
            @Override
            public void OnItemChildrenClickListener(View v, int position) {
                Intent intent = new Intent();
                intent.setClass(context, ImagePreviewActivity.class);
                ArrayList<WorkOrderPhotosDto> listPhoto = new ArrayList<>();
                List<WorkOrderPhotosDto> photos = listWorkOrder.get(position).getPhotos();
                for (int i = 0; i < photos.size(); i++) {
                    listPhoto.add(new WorkOrderPhotosDto(photos.get(i).getWorkOrderId(),OKHttpUtil.NET_URL + "/api/utils/images/400/" + photos.get(i).getPhoto()));
                }
                intent.putExtra(AllConstants.WHAT, listPhoto);
                startActivity(intent);
            }
        });
        rvWorkOrder.setLayoutManager(new LinearLayoutManager(context));
        rvWorkOrder.setAdapter(adapterWorkOrder);
    }

    @Override
    protected void loadData() {
        query.init();
        mPresenter.getData();
    }

    @Override
    protected void search() {
        if (thread != null) thread.interrupt();
        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    while (isThread) {
                        if (isShow) {
                            query.init();
                            mPresenter.getData();
                        }
                        Thread.sleep(10 * 1000);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();
    }

    @Override
    public void onGetDataSuccess(ApiResultData<InsightDto> apiResult) {
        refreshSmartRefreshLayout();
        if (apiResult.getStatus() == 2000) {
            InsightDto data = apiResult.getData();
            list.clear();
            list.addAll(data.getParamDashList());
            adapterResult.notifyDataSetChanged();
            listShow.clear();
            listShow.addAll(data.getParamList());
            adapterShow.notifyDataSetChanged();
            mData.clear();
            mData.addAll(data.getStatusRateList());
            adapter.notifyDataSetChanged();
            listWorkOrder.clear();
            listWorkOrder.addAll(data.getWorkOrderList());
            adapterWorkOrder.notifyDataSetChanged();
        } else if (apiResult.getStatus() == 4001) { // token过期不再继续循环请求
            isShow = false;
        }
    }

    @Override
    public void onGetDataFails(Exception e) {
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (thread != null) {
            thread.interrupt();
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        isShow = !hidden;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(Event event) {
        if (event.getWhat() == 400) {
            isShow = event.isShow();
        }
    }
}
